package data_structure;

public class leetcode_0025 {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null) return null;
        // 区间 [a, b) 包含 k 个待反转元素
        ListNode a, b;
        a = b = head;
        for (int i = 0; i < k; i++) {
            // 不足 k 个，不需要反转，base case
            if (b == null) return head;
            b = b.next;
        }
        // 反转前 k 个元素
        ListNode newHead = reverse(a, b);
        // 递归反转后续链表并连接起来
        a.next = reverseKGroup(b, k);
        return newHead;
    }

    public ListNode reverse(ListNode a,ListNode b){
        ListNode pre = null;
        ListNode current = a;
        ListNode nxt = a;
        while(current !=b){
            nxt = current.next;
            current.next = pre;
            pre = current;
            current = nxt;
        }
        return pre;
    }
}
